<?php

namespace Platform\Event;

use User\Api\UserApi;

class UserEvent {

	public function login($app, $data) {
		$username = $app['app_code'] . '_' . $data['id'];
		$userApi = service('Users');
		$user = $userApi->info($username, true);
		if ($user == -1) {
			$password = rand(10000000, 99999999);
			$nickname = $app['app_code'] . '_' . $data['id'];
			$extend = [
				'avatar' => $data['avatar'],
				'platform' => $app['app_id'],
			];
			$uid = $userApi->register($username, $password, '', $nickname, $extend);
			$user = ['id' => $uid, 'username' => $username];
		}
		$uid = $user['id'];
		$tokenMdl = service('Token');
		$imie = $app['app_code'] . '_' . $data['nickname'];
		$res = $tokenMdl->addToken($uid, $imie, $appToken, $app['app_id']);
		return $appToken;
	}

}